<template>
  <div>
    <div>
      <el-button
        v-waves
        class="filter-item"
        style="margin-left: 10px;margin-top: 20px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        添加
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="购物车用户" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.users.username }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="产品名称"
        prop="name"
        sortable="custom"
        align="center"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.product.proName }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="产品图片"
        prop="tel"
        sortable="custom"
        align="center"
        width="180"
      >
        <template slot-scope="scope">
          <span><img width="200" :src="scope.row.product.proPic">
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="商品数量"
        prop="company"
        sortable="custom"
        align="center"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.count }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总价" prop="sum" sortable="custom" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.sum }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>

          <el-button
            v-if="row.status!='deleted'"
            size="mini"
            type="danger"
            @click="handleDel(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="100px"
        style="width: 400px;
               margin-left:50px;"
      >

        <el-form-item label="商品数量" prop="count">
          <el-input v-model="temp.count" />
          <el-input v-model="temp.id" type="hidden" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import ajax from '@/utils/baseurl'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'

var p

export default {

  name: 'Driver',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      temp: {
        count: '',
        id: ''
      },
      rules: {

      },

      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      listQuery: {
        page: 1,
        limit: 10,
        sort: '+id'
      },
      total: 0,
      tableKey: 0,
      listLoading: true,
      list: null
    }
  },
  created() {
    p = this
    // console.log(ajax);
    this.getList()
  },
  methods: {
    updateData() {
      this.createData()
    },
    resetTemp() {
      this.temp = {
        count: '',
        id: ''
      }
    },
    createData() {
      // this.resetTemp()
      var p = this
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          ajax('updateGouWuChe', this.temp, function(data) {
            if (data) {
              p.dialogFormVisible = false
              p.getList()
              p.$notify({
                title: '搞定',
                message: p.dialogStatus === 'create' ? '添加成功！' : '修改成功！',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDel(row) {
      var p = this
      this.$confirm('此操作将永久删除购物车, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ajax('deleteGouWuChe', { id: row.id }, function(data) {
          if (data) {
            p.getList()
            p.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getList() {
      this.listLoading = true
      ajax('findGouWuChe', this.listQuery, function(data) {
        console.log(data)
        data = JSON.parse(data)
        p.list = data.page.list
        p.total = data.page.totalCount
        p.listLoading = false
      })
    }
  }
}
</script>
